//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ccy.ccysolarcount.config;

import ccy.ccylibrary.config.annotation.FileConfig;
import ccy.ccylibrary.config.annotation.Instance;
import ccy.ccylibrary.config.annotation.none;
import ccy.ccylibrary.data.DataDefault;
import ccy.ccylibrary.data.DefaultData;
import ccy.ccylibrary.data.Mysql;
import ccy.ccylibrary.data.Sqlite;
import ccy.ccylibrary.plugin.annotation.Config;
import ccy.ccylibrary.plugin.annotation.init;
import ccy.ccysolarcount.playerdata.PlayerData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

@Config
public class CcyConfig {
    @Instance
    public static CcyConfig config;
    public int RankTime;
    public int RankLimit;
    public String RankOut;
    @Mysql
    @Sqlite(
            TableField = {"tyn1 INT(16)", "tyn2 INT(16)"}
    )
    @DataDefault
    public DefaultData data;
    @FileConfig
    public FileConfiguration fileConfiguration;
    public Map<Integer, PlayerInfo> mapRank_tyn1;
    public Map<Integer, PlayerInfo> mapRank_tyn2;
    public Map<String, Integer> mapPlayer_tyn1;
    public Map<String, Integer> mapPlayer_tyn2;
    @none
    public Map<UUID, Integer> temp_tyn1;
    @none
    public Map<UUID, Integer> temp_tyn2;

    public CcyConfig() {
    }

    @init
    public void init() {
        this.mapRank_tyn1 = new HashMap<>();
        this.mapRank_tyn2 = new HashMap<>();
        this.mapPlayer_tyn1 = new HashMap<>();
        this.mapPlayer_tyn2 = new HashMap<>();
        this.temp_tyn1 = new HashMap<>();
        this.temp_tyn2 = new HashMap<>();
        this.updateRank();
    }

    public void save() {
        this.temp_tyn1.forEach((uid, num) -> {
            Player player = Bukkit.getPlayer(uid);
            if (player != null && player.isOnline()) {
                (new PlayerData(player)).setTyn1(num);
            }

        });
        this.temp_tyn2.forEach((uid, num) -> {
            Player player = Bukkit.getPlayer(uid);
            if (player != null && player.isOnline()) {
                (new PlayerData(player)).setTyn2(num);
            }

        });
        this.temp_tyn1.clear();
        this.temp_tyn2.clear();
    }

    public void updateRank() {
        this.save();
        this.data.getSql().connect();
        this.updateData("tyn1", this.mapRank_tyn1, this.mapPlayer_tyn1);
        this.updateData("tyn2", this.mapRank_tyn2, this.mapPlayer_tyn2);
        this.data.getSql().closeConnect();
    }

    private void updateData(String value, Map<Integer, PlayerInfo> rank, Map<String, Integer> player) {
        rank.clear();
        player.clear();
        ResultSet r = this.data.getSortResult(value, this.RankLimit);

        try {
            int i = 0;

            while(r.next()) {
                ++i;
                rank.put(i, new PlayerInfo(r.getString("name"), r.getInt(value)));
                player.put(r.getString("name"), i);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
